import time
from selenium import webdriver
from teacher_code import yan_zheng_ma

options = webdriver.ChromeOptions()
options.add_argument("--user-data-dir=C:/Users/20486/AppData/Local/Google/Chrome/User Data/Default")
options.add_experimental_option('excludeSwitches', ['enable-automation'])
options.add_argument("--disable-blink-features=AutomationControlled")
driver = webdriver.Chrome(options=options)
driver.maximize_window()
driver.get("https://jzsc.mohurd.gov.cn/data/company")
time.sleep(5)
tr_list = driver.find_elements("xpath", "//table[@class='el-table__body']//tr")
for tr in tr_list:
    td_list = tr.find_elements("xpath", ".//td")
    data = [td.text for td in td_list]
    print(data)
    # 滚动到特定元素
    driver.execute_script("arguments[0].scrollIntoView();", tr)
    time.sleep(3)
    # 确定打开新标签页生效
    while len(driver.window_handles) == 1:
        # 进入详情页
        td_list[2].click()
        time.sleep(2)

    # 切换新窗口
    driver.switch_to.window(driver.window_handles[-1])
    time.sleep(5)
    try:
        if driver.find_elements("xpath", "//tbody[tr]"):
            addr = driver.find_element("xpath", "//tbody/tr[@class='el-table__row'][1]/td[@class='el-table_1_column_2 is-center ']/div[@class='cell']").text
            print(addr)
        else:
            print("暂无")
    except Exception as e:
        print("11111")
        if driver.find_elements("xpath", "//tbody[tr]"):
            addr = driver.find_element("xpath", "//tbody/tr[@class='el-table__row'][1]/td[@class='el-table_1_column_2 is-center ']/div[@class='cell']").text
            print(addr)
    driver.close()
    # 切换新窗口
    driver.switch_to.window(driver.window_handles[0])
